package com.meraklisina.latexeditor;

import java.io.*;

public class LogFileExaminer {

	private String logFileNameWPath;
	
	
	public LogFileExaminer()
	{
		logFileNameWPath = "";
	}
	
	public LogFileExaminer(String name)
	{
		logFileNameWPath = name;
	}
	
	
	/**
	 * Gets the error message and line number of the error from the LOG file
	 * @return error message and line number of the error 
	 *         
	 */
	public String[] ReadLogFile()
	{
			String[] res = new String[2];
			InputStream is = null;
			BufferedInputStream bis = null;
			String error = "";
			try
			{
				is = new FileInputStream(logFileNameWPath);
				bis = new BufferedInputStream(is, 10);
				String possibleErrorText = "";
				int b;
				boolean flag1 = false;
				boolean flag2 = false;
				
				String lineStr = "";
				
				int count =0;
				char prevLetter = ' ';
				while((b = bis.read()) != -1)
				{
					char letter = (char) b;
					
					if(letter == '!')
					{
						possibleErrorText = "";
					}


					if(flag1 == true)
					{
						
						if(letter != '!' && flag2 == false )
						{
							
							lineStr += String.valueOf(letter);
							
						
							if(lineStr.matches("Here is how much of TeX's memory you used:") == true )
							{
								break;
							}
							else
							{
								
								
							}
							if(letter == '\n')
							{
								error += lineStr;
								lineStr = "";
									

							}

						
						}
						

	
					}
					
					if(letter == '!' && prevLetter == '\n')
					{
						if(flag1 == false)
						{
							flag1 = true;
						}
						else if(flag2 == false)
						{
							flag2 = true;
						}
						
				}
				

					
					

				prevLetter = letter;	
				}
				
				int lineNo = 0;
				if(flag1 == true)
				lineNo = GetLineNumber(error);
				
				if(lineNo> 0)
				{
					error = error.replaceAll("l."+lineNo, "");
					error = error.replaceAll("\n", "");
					
				}
				res[0] = error;
				res[1] = String.valueOf(lineNo);
				
				
			}
			
			catch(IOException exIo)
			{
				
			}
			return res;
		
	}

	
	/**
	 * Gets the line number of the error from the error text
	 * @param errorMsg Error text
	 * @return Line number of the error 
	 *         
	 */
	public int GetLineNumber(String errorMsg)
	{
	int lineNumber = 0;
		int k = errorMsg.indexOf("l.");
		int length = errorMsg.length();
		if(k == -1)
		{
			lineNumber = -1;
		}
		else
		{
			String lineNstr = "";
			String subStr = errorMsg.substring(k+2);
			int subStrL = subStr.length();
			for(int i= 0;i < subStrL-1 ; i++)
			{
			
				String subStr2 = subStr.substring(i,i+1);
				/*
				if(subStr2.matches(" "))
				{
					continue;
				}
				*/
				try
				{
					int n= Integer.parseInt(subStr2);
					lineNstr += subStr2; 
				}
				catch(Exception ex)
				{
					lineNumber = -1;
					break;
				}

			}
			
			if(lineNstr.matches("") == false)
			{
				lineNumber	= Integer.parseInt(lineNstr);
			}
		
		}
		
		return lineNumber;
	}
}
